package com.cskaoyan.service.wxfree;


import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.user.WxUserLoginData;
import com.cskaoyan.bean.wxfree.WxFeedback;
import com.cskaoyan.bo.wxfree.WxFbSubmitBo;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.mapper.WxFeedbackMapper;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-12 09:25
 **/
// 微信端反馈模块
@Service
public class WxFeedbackServiceImpl implements WxFeedbackService {
    @Autowired
    WxFeedbackMapper wxFeedbackMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public WxFeedback fbSubmit(WxFbSubmitBo wxFbSubmitBo) {
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        // 类型转换
        WxFeedback wxFeedback = new WxFeedback();
        Converter converter = PojoUtils.converter();
        converter.converter(wxFbSubmitBo, wxFeedback);
        // 设置一下参数
        wxFeedback.setAddTime(new Date());
        wxFeedback.setUpdateTime(new Date());
        wxFeedback.setDeleted(false);
        if (wxFbSubmitBo.getPicUrls().length >= 1) {
            wxFeedback.setHasPicture(true);
        } else if (wxFbSubmitBo.getPicUrls() == null) {
            wxFeedback.setHasPicture(false);
        }

        // 查询用户信息
        User user = userMapper.selectByPrimaryKey(userId);
        wxFeedback.setUserId(user.getId());
        wxFeedback.setUsername(user.getNickname());

        // 添加反馈信息
        wxFeedbackMapper.insertSelective(wxFeedback);
        return wxFeedback;
    }
}
